//
//  PaomaView.m
//  TengyunStation
//
//  Created by Johnny on 15/8/21.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "PaomaView.h"
#import "AdModelGroup.h"

@interface PaomaView () {
    NSDateFormatter *_dateFormatter;
}

@property(nonatomic,strong)NSString *content;
@property(nonatomic,strong)AdModel *adModel;

@end

@implementation PaomaView

- (void)awakeFromNib {
    self.text = @"";
    self.clipsToBounds = YES;
    self.userInteractionEnabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pullWebVC)];
    tap.numberOfTapsRequired = 1;
    tap.cancelsTouchesInView = YES;
    [self addGestureRecognizer:tap];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

- (void)clearTimer {
    [_timer invalidate];
}

- (void)setAdGroup:(AdModelGroup *)adGroup {
    _adGroup = adGroup;
    AdModel *model = [adGroup.adList firstObject];
    if ([[self currentTime] compare:model.endPlayTime] == NSOrderedAscending) {
        [self clearTimer];
        _timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(checkTimeIsOver) userInfo:nil repeats:YES];
        self.adModel = model;
    } else {
        self.adModel = nil;
    }
}

- (void)checkTimeIsOver {
    if ([[self currentTime] compare:self.adModel.endPlayTime] != NSOrderedAscending) {
        [_timer invalidate];
        self.adModel = nil;
    }
}

- (void)setAdModel:(AdModel *)adModel {
    _adModel = adModel;
    if (adModel.link.length > 0) {
        self.userInteractionEnabled = YES;
    } else {
        self.userInteractionEnabled = NO;
    }
    self.content = adModel.content;
}

- (NSString *)currentTime {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    return [_dateFormatter stringFromDate:[NSDate date]];
}

- (void)pullWebVC {
    if (self.adModel.link.length > 0) {
        if ([_delegate respondsToSelector:@selector(jumpWebViewWithModel:)]) {
            [_delegate jumpWebViewWithModel:self.adModel];
        }
    }
}

- (void)setContent:(NSString *)content {
    _content = content;
    [self clearPaomaView];
    CGSize sizeOfContent = [content sizeWithFont:self.font];
    if (sizeOfContent.width > self.frame.size.width) {
        _rectMark1 = CGRectMake(0, 0, sizeOfContent.width, self.bounds.size.height);
        _labelList = [NSMutableArray arrayWithCapacity:2];
        UILabel *label0 = [[UILabel alloc] initWithFrame:_rectMark1];
        label0.textColor = self.textColor;
        label0.font = self.font;
        label0.text = content;
        label0.userInteractionEnabled = NO;
        [self addSubview:label0];
        _rectMark2 = CGRectMake(_rectMark1.origin.x + _rectMark1.size.width, 0, sizeOfContent.width, self.bounds.size.height);
        UILabel *label1 = [[UILabel alloc] initWithFrame:_rectMark2];
        label1.textColor = self.textColor;
        label1.font = self.font;
        label1.text = content;
        label1.textAlignment = NSTextAlignmentCenter;
        label1.userInteractionEnabled = NO;
        [self addSubview:label1];
        [_labelList addObject:label0];
        [_labelList addObject:label1];
        _speed = content.length/3;
        [self startPaoma];
    } else {
        UILabel *label0 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        label0.textColor = self.textColor;
        label0.font = self.font;
        label0.text = content;
        label0.textAlignment = self.textAlignment;
        label0.userInteractionEnabled = NO;
        [self addSubview:label0];
    }
}

- (void)startPaoma {
    UILabel *label0 = [_labelList objectAtIndex:0];
    UILabel *label1 = [_labelList objectAtIndex:1];
    [UIView transitionWithView:self duration:_speed options:UIViewAnimationOptionCurveLinear animations:^{
        label0.frame = CGRectMake(-_rectMark1.size.width, 0, _rectMark1.size.width, _rectMark1.size.height);
        label1.frame = CGRectMake(label0.frame.origin.x + label0.frame.size.width, 0, label1.frame.size.width, label1.frame.size.height);
    } completion:^(BOOL finished) {
        label0.frame = _rectMark2;
        label1.frame = _rectMark1;
        [_labelList replaceObjectAtIndex:0 withObject:label1];
        [_labelList replaceObjectAtIndex:1 withObject:label0];
        [self startPaoma];
    }];
}

- (void)clearPaomaView {
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
}

- (void)dealloc {
    self.adGroup = nil;
    self.adModel = nil;
}

@end
